<%@ include file="/html/init.jsp" %>

<portlet:renderURL var="addRowURL">
	<portlet:param name="jspPage" value="/html/addRow.jsp"/>
</portlet:renderURL>

<portlet:renderURL var="detailViewURL">
	<portlet:param name="jspPage" value="/html/detailView.jsp"/>
</portlet:renderURL>

<aui:fieldset>
	<aui:a href="<%= addRowURL.toString() %>">ADD</aui:a>
</aui:fieldset>

<portlet:actionURL var="deleteRowURL" name="deleteRows">
	<portlet:param name="tableId" value="<%= String.valueOf(dynaTableId) %>" />
</portlet:actionURL>

<script type="text/javascript">

	function <portlet:namespace />deleteRow(rowsId){
		document.<portlet:namespace />fm.action = '<%= deleteRowURL.toString() %>&rowsId=' + rowsId;
		document.<portlet:namespace />fm.submit();	
	}
	
	function <portlet:namespace />detailView(rowsId){
		document.<portlet:namespace />fm.action = '<%= detailViewURL.toString() %>&rowsId=' + rowsId;
		document.<portlet:namespace />fm.submit();		
	}

    function <portlet:namespace />submitForm(url) {
            var frm = document.<portlet:namespace />fm;
            var count =0 ;
            for (var i=0; i < frm.length; i++) {
                    var ele = frm.elements[i];
                    if (ele.type == 'checkbox' && (ele.name.indexOf('rowsId') == 0) && ele.checked) {
                            count++;
                    }
            }

            if (count <1) {
                    alert("Check Option for delete");
                    return false;
            } else {
                    if (confirm("Are you sure you want to delete the selected record.")) {
                            submitForm(document.<portlet:namespace />fm, url , '',  0);
                    } else {
                            try{
                                    var inputs = jQuery("input[@name=rowsId]", frm);
                                    for(var i =0; i<inputs.length;i++){
                                            inputs[i].disabled=false;
                                    }

                                    frm.checkAll.checked = false;
                                    toggleCheck(frm.checkAll);
                            }catch(err){}
                    }
            }
    }
</script>
 
<%
	PortletURL add_url = renderResponse.createRenderURL();
	add_url.setParameter("jspPage", "/list.jsp");

	List<DynaBean> dynaBeans = DynaTableLocalServiceUtil.getDynaList(themeDisplay.getCompanyId(), dynaTableName);
%>
<form action="" method="post" name="<portlet:namespace />fm" >
   <input class="portlet-form-button button-delete" type="button" value='<liferay-ui:message key="delete" />' onClick="<portlet:namespace />submitForm('<portlet:actionURL><portlet:param name="<%= ActionRequest.ACTION_NAME %>" value="deleteRows" /><portlet:param name="tableId" value="<%= String.valueOf(dynaTableId) %>" /></portlet:actionURL>')" >
   <c:choose>
      <c:when test="<%= dynaBeans.size() > 0%>"> 
<%
	List<String> headers = DynaTableLocalServiceUtil.getDynaPropertys(dynaBeans.get(0));
	String checkBox = "<input type='checkbox' name='checkAll' onClick=\"javascript:Liferay.Util.checkAll(this.form,'rowsId',this)\">";
	headers.add(0, checkBox);
	
    SearchContainer searchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, dynaBeans.size(), add_url, headers, null);
    List resultRows = searchContainer.getResultRows();
    searchContainer.getIteratorURL().setParameter("cur" , String.valueOf(searchContainer.getCurValue()));
    searchContainer.setTotal(dynaBeans.size());
    dynaBeans = ListUtil.subList(dynaBeans, searchContainer.getStart(), searchContainer.getEnd());
    searchContainer.setResults(dynaBeans);	
    
    for (int i=0;i<dynaBeans.size();i++){
    	DynaBean dynaBean = (DynaBean)dynaBeans.get(i);
    	String classPK = String.valueOf(dynaBean.get("classPK"));
    	ResultRow row = new ResultRow(dynaBean, classPK, i);
        String chckBox = "<input type=checkbox name='rowsId' value='" + classPK +"' onClick=\"javascript:Liferay.Util.checkAllBox(this.form,'rowsId',this.form.checkAll)\">";
        row.addText(chckBox);
        for (String header : headers){
        	if(checkBox.equalsIgnoreCase(header)) continue;
        	row.addText(String.valueOf(dynaBean.get(header)));
        }
        row.addJSP("/html/actionTab.jsp", this.getServletContext(), request, response);
        resultRows.add(row);
    }
%>
  		<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />
	   </c:when>
	   <c:otherwise>
	      	<br>Sorry. There are no items to display.
	   </c:otherwise>
   </c:choose>
</form>   